/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.portlet.editor.gui.view.settings.panel.portlet;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import com.genia.toolbox.portlet.editor.gui.PortletEditorGUI;
import com.genia.toolbox.portlet.editor.gui.view.settings.table.HoleCellEditor;
import com.genia.toolbox.portlet.editor.gui.view.settings.table.HoleCellRenderer;
import com.genia.toolbox.portlet.editor.gui.view.settings.table.HoleTableModel;
import com.genia.toolbox.portlet.editor.model.portlet.PortletType;
import com.genia.toolbox.portlet.editor.model.portlet.impl.HoleModel;
import com.genia.toolbox.portlet.editor.model.portlet.impl.PortletModel;
import com.genia.toolbox.portlet.editor.model.portlet.impl.TemplatePortletModel;

/**
 * The template settings panel.
 */
@SuppressWarnings("serial")
public class TemplatePortletSettingsPanel
    extends AbstractPortletSettingsPanel
{

  /**
   * The hole label.
   */
  private JLabel labelHole = null;

  /**
   * The holes table.
   */
  private JTable tableHoles = null;

  /**
   * The holes table model.
   */
  private HoleTableModel tableHoleModel = null;



  /**
   * Constructor.
   * 
   * @param portletEditorGUI
   *          the application portletEditorGUI.
   */
  public TemplatePortletSettingsPanel(PortletEditorGUI portletEditorGUI)
  {
    super(portletEditorGUI);
  }



  /**
   * Initialise the panel.
   */
  @Override
  public void initialise()
  {
    super.initialise();
    this.getTextId().setEnabled(false);
  }



  /**
   * Create the specific panel.
   * 
   * @return the specific panel.
   */
  @Override
  public JPanel createSpecificPanel()
  {
    JPanel panel = new JPanel();

    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    panel.setLayout(layout);

    this.labelHole = new JLabel(this.getGui().getController().notifyTranslation("com.genia.toolbox.portlet.editor.gui.view.settings.panel.portlets.TemplatePortletSettingsPanel.name"));
    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.gridwidth = 3;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.insets = new Insets(0, 0, 0, 0);
    panel.add(this.labelHole, constraints);

    String[] columnNames = new String[] {
        this.getGui().getController().notifyTranslation("com.genia.toolbox.portlet.editor.gui.view.settings.panel.portlets.TemplatePortletSettingsPanel.column.name"),
        this.getGui().getController().notifyTranslation("com.genia.toolbox.portlet.editor.gui.view.settings.panel.portlets.TemplatePortletSettingsPanel.column.portlet") };
    this.tableHoleModel = new HoleTableModel(columnNames);
    this.tableHoleModel.addTableModelListener(this);
    this.tableHoles = new JTable(this.tableHoleModel);
    List<String> portlets = this.getGui().getController().notifyDisplayPortlets();
    HoleCellRenderer cellRenderer = new HoleCellRenderer();
    HoleCellEditor cellEditor = new HoleCellEditor(portlets);
    this.tableHoles.getColumnModel().getColumn(0).setCellRenderer(cellRenderer);
    this.tableHoles.getColumnModel().getColumn(1).setCellRenderer(cellRenderer);
    this.tableHoles.getColumnModel().getColumn(0).setCellEditor(cellEditor);
    this.tableHoles.getColumnModel().getColumn(1).setCellEditor(cellEditor);
    this.tableHoles.setRowHeight(25);
    this.tableHoles.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    JScrollPane scrollpane = new JScrollPane(this.tableHoles);
    constraints.gridx = 1;
    constraints.gridy = 2;
    constraints.gridwidth = 3;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 1;
    constraints.fill = GridBagConstraints.BOTH;
    constraints.insets = new Insets(5, 15, 0, 0);
    panel.add(scrollpane, constraints);

    return panel;
  }



  /**
   * Set the displayed portlet.
   * 
   * @param model
   *          the portlet model.
   */
  @Override
  public void setPortlet(PortletModel model)
  {
    this.removeAllListeners();
    super.setPortlet(model);
    this.hideIdentifier();
    TemplatePortletModel templateModel = (TemplatePortletModel) model;
    this.tableHoleModel.clear();
    for (HoleModel hole : templateModel.getHoles()) {
      this.tableHoleModel.addHole(hole);
    }
    this.textPortletName.setText(PortletType.Template.name());
    this.areaDescription.setText(this.getGui().getController().notifyTranslation("com.genia.toolbox.portlet.editor.gui.dialog.PortletSelectorDialog.area.template"));
    this.addAllListeners();
    this.validateSettings();
  }



  /**
   * The displayed portlet model.
   * 
   * @return the displayed portlet model.
   */
  @Override
  public PortletModel getPortlet()
  {
    TemplatePortletModel updatedModel = (TemplatePortletModel) super.getPortlet();
    List<HoleModel> holes = new ArrayList<HoleModel>();
    int rows = this.tableHoles.getRowCount();
    for (int row = 0; row < rows; row++) {
      HoleModel hole = this.tableHoleModel.getValueAt(row);
      if (hole != null) {
        holes.add(hole);
      }
    }
    updatedModel.setHoles(holes);
    return updatedModel;
  }



  /**
   * Remove all listeners.
   */
  @Override
  public void removeAllListeners()
  {
    super.removeAllListeners();
    if (this.tableHoleModel != null) {
      this.tableHoleModel.removeTableModelListener(this);
    }
  }



  /**
   * Add all listeners.
   */
  @Override
  public void addAllListeners()
  {
    super.addAllListeners();
    if (this.tableHoleModel != null) {
      this.tableHoleModel.addTableModelListener(this);
    }
  }

}
